package com.lixiaoyi.emp.jsp;

import com.lixiaoyi.emp.entity.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author 李小毅
 * @Description
 * @Date 2022-05-12
 */
@WebServlet("/manager/safe/showAllEmpJSP")
public class showAllEmpJSP extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.获取集合数据
        List<Emp> emps =  (List<Emp>)request.getAttribute("emps");
        //2.打印数据前处理乱码
        PrintWriter printWriter = response.getWriter();
        //界面
        printWriter.println("<html>");
        printWriter.println("   <head>");
        printWriter.println("       <meta charset='utf-8'>");
        printWriter.println("       <title>查询所有员工信息</title>");
        printWriter.println("   </head>");
        printWriter.println("       <body>");
        printWriter.println("           <table border=2>");
        printWriter.println("               <tr>");
        printWriter.println("                   <td>编号</td>");
        printWriter.println("                   <td>姓名</td>");
        printWriter.println("                   <td>年龄</td>");
        printWriter.println("                   <td>工资</td>");
        printWriter.println("                   <td colspan='2'>操作</td>");
        printWriter.println("               </tr>");
        for (Emp emp : emps) {
            printWriter.println("               <tr>");
            printWriter.println("                   <td>"+ emp.getId() +"</td>");
            printWriter.println("                   <td>"+ emp.getName() +"</td>");
            printWriter.println("                   <td>"+ emp.getAge() + "</td>");
            printWriter.println("                   <td>"+ emp.getSalary() + "</td>");
            printWriter.println("                   <td><a href='"+request.getContextPath() + "/manager/safe/showEmpController?id="+emp.getId() +"'>修改</a></td>");
            printWriter.println("                   <td><a href='"+request.getContextPath() + "/manager/safe/removeEmpController?id="+emp.getId() +"'>删除</a></td>");
            printWriter.println("                   <td><a href='"+request.getContextPath() + "/manager/safe/addEmpController" +"'>添加</a></td>");
            printWriter.println("               </tr>");
        }
        printWriter.println("           </table>");
        printWriter.println("       </body>");
        printWriter.println("</html>");

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }
}
